package com.tencent.bugly.beta.utils;

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import net.lingala.zip4j.util.InternalZipConstants;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class a {
    public static long a = -1;
    public static long b = 0;
    public static long c = 1;
    private byte[] d;
    private byte[] e;
    private byte[] f;
    private long g;
    private String h;
    private BufferedInputStream i;
    private long j;
    private long k;

    public synchronized void a(long j) {
        this.g = j;
    }

    public a(String str) throws Exception {
        this.d = new byte[2];
        this.e = new byte[4];
        this.f = new byte[8];
        this.g = c;
        this.h = null;
        this.i = null;
        this.j = 0L;
        this.k = 0L;
        this.h = str;
        this.i = new BufferedInputStream(new FileInputStream(this.h));
        this.j = 0L;
        this.k = 0L;
    }

    public a(String str, long j) throws Exception {
        this.d = new byte[2];
        this.e = new byte[4];
        this.f = new byte[8];
        this.g = c;
        this.h = null;
        this.i = null;
        this.j = 0L;
        this.k = 0L;
        this.h = str;
        this.g = j;
        this.i = new BufferedInputStream(new FileInputStream(this.h));
        this.j = 0L;
        this.k = 0L;
    }

    public synchronized boolean a() {
        try {
            if (this.i != null) {
                this.i.close();
            }
            this.i = null;
            this.h = null;
            this.j = 0L;
            this.k = 0L;
        } catch (IOException e) {
            Log.e("BinaryFileReader", e.getMessage());
            return false;
        }
        return true;
    }

    public synchronized boolean b(long j) {
        if (this.i == null) {
            Log.e("BinaryFileReader", "Please open file first！");
            return false;
        } else if (j == 0) {
            return true;
        } else {
            long j2 = j;
            while (j2 > 0) {
                try {
                    j2 -= this.i.skip(j2);
                } catch (IOException unused) {
                    Log.e("BinaryFileReader", "Failed to skip file pointer！");
                    return false;
                }
            }
            this.j += j;
            return true;
        }
    }

    public synchronized boolean a(byte[] bArr) {
        try {
            this.i.read(bArr);
            this.j += bArr.length;
            this.k += bArr.length;
        } catch (IOException e) {
            Log.e("BinaryFileReader", e.getMessage());
            return false;
        }
        return true;
    }

    public synchronized byte b() throws IOException {
        if (this.i == null) {
            Log.e("BinaryFileReader", "Failed to skip file pointer！");
            return (byte) 0;
        }
        byte read = (byte) this.i.read();
        this.j++;
        this.k++;
        return read;
    }

    public synchronized short c() throws IOException {
        if (this.i == null) {
            Log.e("BinaryFileReader", "Failed to skip file pointer！");
            return (short) 0;
        }
        this.i.read(this.d);
        short a2 = a(this.d, this.g);
        this.j += 2;
        this.k += 2;
        return a2;
    }

    public synchronized int d() throws IOException {
        if (this.i == null) {
            Log.e("BinaryFileReader", "Failed to skip file pointer！");
            return 0;
        }
        this.i.read(this.e);
        int b2 = b(this.e, this.g);
        this.j += 4;
        this.k += 4;
        return b2;
    }

    public synchronized long e() throws IOException {
        if (this.i == null) {
            Log.e("BinaryFileReader", "Failed to skip file pointer！");
            return 0L;
        }
        this.i.read(this.f);
        long c2 = c(this.f, this.g);
        this.j += 8;
        this.k += 8;
        return c2;
    }

    public synchronized long f() throws IOException {
        return b() & 255;
    }

    public synchronized long g() throws IOException {
        return c() & 65535;
    }

    public synchronized long h() throws IOException {
        return d() & InternalZipConstants.ZIP_64_LIMIT;
    }

    public synchronized long i() throws IOException {
        return e();
    }

    private static short b(byte[] bArr) {
        if (bArr == null || bArr.length > 2) {
            return (short) -1;
        }
        return (short) c(bArr);
    }

    private static int c(byte[] bArr) {
        if (bArr == null || bArr.length > 4) {
            return -1;
        }
        return (int) d(bArr);
    }

    private static long d(byte[] bArr) {
        if (bArr == null || bArr.length > 8) {
            return -1L;
        }
        long j = 0;
        for (int length = bArr.length - 1; length >= 0; length--) {
            j = (j << 8) | (bArr[length] & 255);
        }
        return j;
    }

    private static short e(byte[] bArr) {
        if (bArr == null || bArr.length > 2) {
            return (short) -1;
        }
        return (short) f(bArr);
    }

    private static int f(byte[] bArr) {
        if (bArr == null || bArr.length > 4) {
            return -1;
        }
        return (int) g(bArr);
    }

    private static long g(byte[] bArr) {
        if (bArr == null || bArr.length > 8) {
            return -1L;
        }
        long j = 0;
        for (byte b2 : bArr) {
            j = (j << 8) | (b2 & 255);
        }
        return j;
    }

    public static short a(byte[] bArr, long j) {
        if (j == c) {
            return b(bArr);
        }
        return e(bArr);
    }

    public static int b(byte[] bArr, long j) {
        if (j == c) {
            return c(bArr);
        }
        return f(bArr);
    }

    public static long c(byte[] bArr, long j) {
        if (j == c) {
            return d(bArr);
        }
        return g(bArr);
    }
}
